package com.pic4493.app;

import java.net.URLDecoder;

import org.json.JSONObject;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.pic4493.base.BaseUITopBar;
import com.pic4493.comm.PubConst;
import com.pic4493.comm.Session;
import com.pic4493.controls.CstProgressWebView;
import com.pic4493.entities.EUser;
import com.pic4493.utils.UtiDialog;

public class UIWebLogin extends BaseUITopBar
{
   private CstProgressWebView webView;
   private int mLoginType;

   private void init()
   {
	  webView = (CstProgressWebView) findViewById(R.id.uiweblogin_webview);
	  webView.getSettings().setJavaScriptEnabled(true);
	  webView.getSettings().setBuiltInZoomControls(true);
	  webView.getSettings().setDefaultTextEncodingName("utf-8");
	  webView.setWebViewClient(new WeiboWebViewClient());
	  switch (mLoginType)
	  {
		 case PubConst.LoginType.WEIBO:
			  webView.loadUrl("http://api.4493.com/api/home/login/wblogin");
			break;
		 case PubConst.LoginType.WEIXIN:

			break;
		 case PubConst.LoginType.RENREN:

			break;
	  }
   }

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	  setContentView(R.layout.uiweblogin);
	  super.onCreate(savedInstanceState);
	  setTitle("登录");
	  
	  mLoginType=getIntent().getIntExtra(PubConst.Intent_Key.LoginType, 1);
	  init();
   }

   private class WeiboWebViewClient extends WebViewClient
   {
	  @Override
	  public boolean shouldOverrideUrlLoading(WebView view, String url)
	  {
		 if (url.indexOf("login") == 0)// 短信======================
		 {
			String temp;
			try
			{
			   temp = URLDecoder.decode(url, "UTF-8");
			   String message = temp.replace("login://", "");
			   JSONObject json = new JSONObject(message);
			   EUser user = new EUser();
			   user.setNickName(json.optString("nickname"));
			   user.setPassWord(json.optString("passwd"));
			   user.setUserId(json.optString("uid"));
			   user.setUserName(json.optString("username"));
			   user.setNewsCount(json.optInt("msgCount"));
			   user.setHeadImage(json.optString("avatars"));
			   Session.getInstance().setUser(user);
			   UtiDialog.getInstance().ShowToastInfoShort(mContext, "登陆成功");
			   setResult(RESULT_OK);
			   finish();
			}
			catch (Exception e)
			{
			   Log.d("ERROR", "UIWebLogin.WeiboWebViewClient_shouldOverrideUrlLoading(view, url) " + e);
			}
			return true;
		 }
		 return super.shouldOverrideUrlLoading(view, url);
	  }

	  @Override
	  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
	  {
		 super.onReceivedError(view, errorCode, description, failingUrl);
	  }

	  @Override
	  public void onPageStarted(WebView view, String url, Bitmap favicon)
	  {
		 super.onPageStarted(view, url, favicon);
	  }

	  @Override
	  public void onPageFinished(WebView view, String url)
	  {

	  }

	  public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
	  {
		 handler.proceed();
	  }
   }
}
